package com.guit.hrms.utils;

import com.guit.hrms.po.ToEmail;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;


    /**
     * 发送简单文本邮件
     * @param toEmail 邮件信息
     */
    public void sendSimpleEmail(ToEmail toEmail) {
        try{
            if (toEmail.getTos() == null || toEmail.getTos().length == 0){
                return;
            }
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from); // 发件人邮箱
            message.setTo(toEmail.getTos());     // 收件人邮箱
            message.setSubject(toEmail.getSubject()); // 邮件主题
            message.setText(toEmail.getContent()); // 邮件内容
            mailSender.send(message);
        }catch (Exception e) {
            throw new RuntimeException("邮件发送失败", e);
        }
    }

    /**
     * 发送HTML格式邮件
     * @param toEmail 邮件信息
     */
    public void sendHtmlMail(ToEmail toEmail) throws MessagingException {
        try{
            if (toEmail.getTos() == null || toEmail.getTos().length == 0){
                return;
            }
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(toEmail.getTos());
            helper.setSubject(toEmail.getSubject());
            helper.setText(toEmail.getContent(), true);
            mailSender.send(message);
        }catch (Exception e) {
            throw new RuntimeException("邮件发送失败", e);
        }
    }

    /**
     * 发送HTML带附件格式邮件
     * @param toEmail 邮件信息
     */
    public void sendAttachmentMail(ToEmail toEmail, String offerToken) throws MessagingException {
        try{
            if (toEmail.getTos() == null || toEmail.getTos().length == 0){
                return;
            }
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(toEmail.getTos());
            helper.setSubject(toEmail.getSubject());
//            helper.setText(toEmail.getContent());
            helper.setText(html(toEmail.getContent(),offerToken), true);
            helper.addAttachment("offer.pdf", toEmail.getOffer());
            mailSender.send(message);
        }catch (Exception e) {
            throw new RuntimeException("邮件发送失败", e);
        }
    }

    public String html(String content, String token){
        return content + "\n<table role=\"presentation\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 20px auto;\">\n" +
                        "    <tr>\n" +
                        "        <td style=\"padding: 0 10px;\">\n" +
                        "            <a href=\"http://localhost:3000/recruitment/offer/accept/result?token={"+token+"}\" \n" +
                        "               style=\"background: #4CAF50; color: white; padding: 12px 24px; text-decoration: none; border-radius: 5px;\">\n" +
                        "                点击跳转页面选择是否接受Offer\n" +
                        "            </a>\n" +
                        "        </td>\n" +
//                        "        <td style=\"padding: 0 10px;\">\n" +
//                        "            <a href=\"https://localhost:8080/interview/interviewee/offer/result?token={"+token+"}\" \n" +
//                        "               style=\"background: #f44336; color: white; padding: 12px 24px; text-decoration: none; border-radius: 5px;\">\n" +
//                        "                ❌ 拒绝Offer\n" +
//                        "            </a>\n" +
//                        "        </td>\n" +
                        "    </tr>\n" +
                        "</table>"
                + "\n<table role=\"presentation\" cellspacing=\"0\" cellpadding=\"0\" style=\"margin: 20px auto;\">\n" +
                "    <tr>\n" +
                "        <td style=\"padding: 0 10px;\">\n" +
                "            <a href=\"http://linchaochao.com/recruitment/offer/accept/result?token={"+token+"}\" \n" +
                "               style=\"background: #4CAF50; color: white; padding: 12px 24px; text-decoration: none; border-radius: 5px;\">\n" +
                "                点击跳转页面选择是否接受Offer\n" +
                "            </a>\n" +
                "        </td>\n" +
//                        "        <td style=\"padding: 0 10px;\">\n" +
//                        "            <a href=\"https://localhost:8080/interview/interviewee/offer/result?token={"+token+"}\" \n" +
//                        "               style=\"background: #f44336; color: white; padding: 12px 24px; text-decoration: none; border-radius: 5px;\">\n" +
//                        "                ❌ 拒绝Offer\n" +
//                        "            </a>\n" +
//                        "        </td>\n" +
                "    </tr>\n" +
                "</table>";
    }

//    public static void main(String[] args) {
//        ToEmail toEmail = new ToEmail();
//        toEmail.setSubject("111");
//        toEmail.setTos(new String[]{"2022512275@qq.com"});
//        toEmail.setContent("hhhhhh");
//        toEmail.setOffer(null);
//        EmailUtil emailUtil = new EmailUtil();
//        try {
//            emailUtil.sendAttachmentMail(toEmail, "qeovnqmeov");
//        } catch (MessagingException e) {
//            throw new RuntimeException(e);
//        }
//    }
}

//    /**
//     * 发送HTML格式邮件
//     *
//     * @param from        发件人邮箱
//     * @param to          收件人邮箱
//     * @param subject     邮件主题
//     * @param htmlContent HTML格式的邮件内容
//     * @throws MessagingException 邮件发送异常
//     */
//    public void sendHtmlEmail(String from, String to, String subject, String htmlContent) throws MessagingException {
//        MimeMessage message = mailSender.createMimeMessage();
//        MimeMessageHelper helper = new MimeMessageHelper(message, true);
//        helper.setFrom(from); // 发件人邮箱
//        helper.setTo(to);     // 收件人邮箱
//        helper.setSubject(subject); // 邮件主题
//        helper.setText(htmlContent, true); // 邮件内容，true表示内容是HTML格式
//        mailSender.send(message);
//    }
